<?php

include_once("../info_struct.php");

/*
 Helper Function for arxiv_get;
get_content_by_name($content, $search);
parameter:
content - the whole content of the div in which should be searched
search - the key to find the searched content after

return:
string - the searched string
false - if search is not found
*/

function a_get_content_by_name($content, $search) {
    $find = '<'.$search.'>';
    $pos = strpos($content, $find);

    if ($pos === false) {
        echo "Error with finding the class";
        return false;
    } else {
        $offset = strpos($content, '</'.$search.'>', $pos);
        if ($offset === false) {
            echo "Error with finding the end div";
            return false;
        } else {
            $str = substr($content, $pos + strlen($find) + 1, $offset - ($pos + strlen($find) + 1));
            return preg_replace("/<[^>]*>/","",$str);
        }
    }
    return false;
}

/*
 Usage:
old_arxiv_get_by_id(id);
paramter:
id - ArXiv Id of the article

return:
class infos
see info_struct.php for content of the class
false - if all went wrong / id not found
*/

function old_arxiv_get_by_id($id) {

    if(function_exists('curl_version')) {
        include_once("../helper_funcs.php");
        $content = curl_file_get_contents("http://export.arxiv.org/api/query?id_list=".$id);
    } else {
        $content = file_get_contents("http://export.arxiv.org/api/query?id_list=".$id);
    }
    if ($content === false)
        return false;

    $pos = strpos($content, '<entry>');
    if ($pos === false) {
        echo "Error with finding the article";
        return false;
    } else {
        $offset = strpos($content, "</entry>", $pos);
        if ($offset === false) {
            echo "Error with finding the end article";
            return false;
        } else {
            $length = $offset - $pos;
            $content = substr($content, $pos, $length);
        }
    }

    $return = new infos();

    $return->title = a_get_content_by_name($content, "title");
    $return->author = a_get_content_by_name($content, "author");
    $return->summary = a_get_content_by_name($content, "summary");
    $return->year = substr(a_get_content_by_name($content, "published"),0,4);
    $return->classification = a_get_content_by_name($content, "classification");

    $return->arxiv = (string)$id;

    $return->from = "arx";

    return $return;
}

/*
 Usage:
arxiv_get_by_id(id);
paramter:
id - ArXiv Id of the article

return:
class infos
see info_struct.php for content of the class
false - if all went wrong / id not found
*/

function arxiv_get_by_id($id) {

    if(function_exists('curl_version')) {
        include_once("../helper_funcs.php");
        $content = curl_file_get_contents("http://export.arxiv.org/api/query?id_list=".$id);
    } else {
        $content = file_get_contents("http://export.arxiv.org/api/query?id_list=".$id);
    }
    if ($content === false)
        return false;

    $xml = simplexml_load_string($content);
    if ($xml === false)
        return false;

    $return = array();

    foreach($xml->entry as $entry) {
        $new_entry = new infos();
        	
        $new_entry->arxiv = preg_replace("/v\d\z/", "", substr((string)$entry->id, 21));
        	
        $new_entry->title = (string)$entry->title;

        $new_entry->year = substr((string)$entry->published, 0, 4);
        	
        $author_array = array();
        foreach ($entry->author as $author) {
            array_push($author_array, (string)$author->name);
        }
        $new_entry->author = implode(", ",$author_array);
        	
        $new_entry->summary = preg_replace("/\n/", "", (string)$entry->summary);

        if (preg_match("/>(.*)<\/arxiv:journal_ref>/", $entry->asXML(), $journal) === 1)
            $new_entry->journal = preg_replace("/\n/", "", $journal[1]);
        	
        $new_entry->from = "arx";
        	
        array_push($return, $new_entry);
    }

    return $return[0];
}

/*
 Usage:
arxiv_get_by_string(string);
paramter:
string - search string

return:
class infos
see info_struct.php for content of the class
false - if all went wrong / id not found
*/

function arxiv_get_by_string($string) {

    $search = str_replace(" ", "%20", $string);
     
    if(function_exists('curl_version')) {
        include_once("../helper_funcs.php");
        $content = curl_file_get_contents("http://export.arxiv.org/api/query?search_query=all:".$search."&start=0&max_results=10");
    } else {
      	 $content = file_get_contents("http://export.arxiv.org/api/query?search_query=all:".$search."&start=0&max_results=10");
    }
    if ($content === false)
        return false;

    $xml = simplexml_load_string($content);
    if ($xml === false)
        return false;

    $return = array();

    foreach($xml->entry as $entry) {
        $new_entry = new infos();
         
        $new_entry->arxiv = preg_replace("/v\d\z/", "", substr((string)$entry->id, 21));
         
        $new_entry->title = (string)$entry->title;
         
        $new_entry->year = substr($entry->published, 0, 4);
         
        $author_array = array();
        foreach ($entry->author as $author) {
            array_push($author_array, (string)$author->name);
        }
        $new_entry->author = implode(", ", $author_array);
         
        $new_entry->summary = preg_replace("/\n/", "", (string)$entry->summary);
         
        if (preg_match("/>(.*)<\/arxiv:journal_ref>/", $entry->asXML(), $journal) === 1)
            $new_entry->journal = preg_replace("/\n/", "", $journal[1]);
         
        $new_entry->from = "arx";
         
        array_push($return, $new_entry);
    }

    return $return;
}
?>